home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_03
/
9n03118a
< prev
next >
Wrap
Text File
|
1991-01-15
|
2KB
|
94 lines
/*
* xrt.c - cross-reference table implementation
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xrt.h"
typedef struct listnode listnode;
struct listnode
{
unsigned number;
listnode *next;
};
typedef struct treenode treenode;
struct treenode
{
char *word;
listnode *lines;
treenode *left, *right;
};
static void addnumber(treenode *t, unsigned n)
{
listnode *p;
p = t->lines;
while (p->next != NULL && p->number != n)
p = p->next;
if (p->number != n)
{
p = p->next
= (listnode *)malloc(sizeof(listnode));
p->number = n;
p->next = NULL;
}
}
static treenode *addtree
(treenode *t, char *w, unsigned n)
{
int cond;
if (t == NULL)
{
t = (treenode *)malloc(sizeof(treenode));
t->word =
strcpy((char *)malloc(strlen(w) + 1), w);
t->lines =
(listnode *)malloc(sizeof(listnode));
t->lines->number = n;
t->lines->next = NULL;
t->left = t->right = NULL;
}
else if ((cond = strcmp(w, t->word)) == 0)
addnumber(t, n);
else if (cond < 0)
t->left = addtree(t->left, w, n);
else
t->right = addtree(t->right, w, n);
return t;
}
static void printtree(treenode *t)
{
listnode *p;
if (t != NULL)
{
printtree(t->left);
printf("%12s: ", t->word);
for (p = t->lines; p != NULL; p = p->next)
printf("%4d ", p->number);
printf("\n");
printtree(t->right);
}
}
static treenode *root = NULL;
void xrt_add(char *w, unsigned n)
{
root = addtree(root, w, n);
}
void xrt_print(void)
{
printtree(root);
}